//
//  baseView.m
//  ImagePreview
//
//  Created by wujungao on 15/6/9.
//  Copyright (c) 2015年 wjg. All rights reserved.
//

#import "BaseView.h"

@interface BaseView()

@end

@implementation BaseView

/*  调用initWithImagesArray:，初始化之后的实例，就拥有多图浏览功能。
    BaseView的作用：实现多图（ImageScroll）、顶部小工具条（topView）和底部bottomView（用于功能扩展）布局；
                   以及整体图片背景设置。
 */
-(UIView *)initWithImagesArray:(NSArray *)imagesArrsy{
    self=[super initWithFrame:[UIScreen mainScreen].applicationFrame];
    if(self){
        [self setScreenValue];
        self.imagesArray=imagesArrsy;
        self.backgroundColor=[UIColor blackColor];
        [self addGestureToBaseView];
        
        [self addTopView];
        [self addImageScroll];
        [self bringSubviewToFront:self.topView];
    }
    return self;
}

-(void)addImageScroll{
    self.scrollView=[[ImageScroll alloc]initWithImagesArray:self.imagesArray];
    self.scrollView.delegate=self;
    [self addSubview:self.scrollView];
}

-(void)setScreenValue{
    self.screenWidth=[UIScreen mainScreen].bounds.size.width;
    self.screenAppHight=[UIScreen mainScreen].applicationFrame.size.height;
}

/* 添加topView */
-(void)addTopView{
    self.isAppeared=YES;//设置滑出滑入标记
    self.topView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, 30)];
    self.topView.backgroundColor=[UIColor clearColor];
    
    [self addBackButton];
    [self addShareButton];
    [self addLabel];
    
    [self addGestureToTopView];
    [self addSubview:self.topView];
}

//addButton
-(void)addBackButton{
    self.backButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame=CGRectMake(5, 0, 30, 30);
    self.backButton.backgroundColor=[UIColor clearColor];
    [self.backButton setTitle:@"B" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.backButton addTarget:self action:@selector(handleShareButton) forControlEvents:UIControlEventTouchDown];
    [self.topView addSubview:self.backButton];
}
-(void)addShareButton{
    self.shareButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.shareButton.frame=CGRectMake((self.screenWidth-35), 0, 30, 30);
    self.shareButton.backgroundColor=[UIColor clearColor];
    [self.shareButton setTitle:@"S" forState:UIControlStateNormal];
    [self.shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.shareButton addTarget:self action:@selector(handleShareButton) forControlEvents:UIControlEventTouchDown];
    [self.topView addSubview:self.shareButton];
}

//点击分享按钮事件
-(void)handleShareButton{
    //
}
//点击返回按钮事件
-(void)handleBackButton{
    //
}

//addLabel
-(void)addLabel{
    self.imageNumberString=[[UILabel alloc] initWithFrame:CGRectMake((self.screenWidth * 0.5), 0, 30, 30)];
    self.imageNumberString.backgroundColor=[UIColor clearColor];
    self.imageNumberString.text=[[NSString alloc] initWithFormat:@"1/%lu",(unsigned long)self.imagesArray.count];//需要添加一个逻辑，用于更新text的值。滑动时，更新值。
    self.imageNumberString.textColor=[UIColor whiteColor];
    [self.topView addSubview:self.imageNumberString];
}
//添加bottomView，用于功能扩展
-(void)addBottomView{
    self.bottomView=[[UIView alloc] initWithFrame:CGRectMake(0, self.screenAppHight-30, self.screenWidth, 30)];
    self.bottomView.backgroundColor=[UIColor clearColor];
    
    [self addSubview:self.bottomView];
}

/* 手势支持 */
-(void)addGestureToTopView{
    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingelTap:)];
    
    [self.topView addGestureRecognizer:singleTap];
}

-(void)addGestureToBaseView{
    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingelTap:)];
    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTap.numberOfTapsRequired=2;
    
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [self addGestureRecognizer:singleTap];
    [self addGestureRecognizer:doubleTap];
}

//单击，滑入滑出topView效果
-(void)handleSingelTap:(UITapGestureRecognizer *)recognizer{
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAppeared){
            self.topView.frame=CGRectMake(0, -30, self.screenWidth, 30);
            self.isAppeared=NO;
        }else{
            self.topView.frame=CGRectMake(0, 0, self.screenWidth, 30);
            self.isAppeared=YES;
        }
        
    }completion:nil];
}

-(void)handleDoubleTap:(UITapGestureRecognizer *)recognizer{
    //双击不做任何事情，用于让BaseView只对单击做出响应。双击时，它的子视图做出响应
}

//更新self.imageNumberString.text值的逻辑。
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index=(self.scrollView.contentOffset.x/self.screenWidth) +1;
    self.imageNumberString.text=[[NSString alloc] initWithFormat:@"%ld/%lu",(long)index,(unsigned long)self.imagesArray.count];
}

@end
